{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "webdriver 提供了一系列的对象定位方法，常用的有以下几种\n",
    " · id\n",
    " · name\n",
    " · class name\n",
    " · link text\n",
    " · partial link text\n",
    " · tag name\n",
    " · xpath\n",
    " · css selector\n"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "我们可以看到，一个百度的输入框，可以用这么用种方式去定位。\n",
    "<input id=\"kw\" class=\"s_ipt\" type=\"text\" maxlength=\"100\" name=\"wd\"\n",
    "autocomplete=\"off\">"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "from selenium import webdriver\n",
    "\n",
    "browser = webdriver.Firefox()\n",
    "browser.get(\"http://www.baidu.com\")\n",
    "\n",
    "#########百度输入框的定位方式##########\n",
    "\n",
    "#通过 id 方式定位\n",
    "browser.find_element_by_id(\"kw\").send_keys(\"selenium\")\n",
    "\n",
    "#通过 name 方式定位\n",
    "browser.find_element_by_name(\"wd\").send_keys(\"selenium\")\n",
    "\n",
    "#通过 tag name 方式定位\n",
    "browser.find_element_by_tag_name(\"input\").send_keys(\"selenium\")\n",
    "\n",
    "#通过 class name 方式定位\n",
    "browser.find_element_by_class_name(\"s_ipt\").send_keys(\"selenium\")\n",
    "\n",
    "#通过 CSS 方式定位\n",
    "browser.find_element_by_css_selector(\"#kw\").send_keys(\"selenium\")\n",
    "\n",
    "#通过 xphan 方式定位\n",
    "browser.find_element_by_xpath(\"//input[@id='kw']\").send_keys(\"selenium\")\n",
    "\n",
    "############################################\n",
    "browser.find_element_by_id(\"su\").click()\n",
    "time.sleep(3)\n",
    "browser.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CSS 定位"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "find_element_by_css_selector(\"#kw\")\n",
    "\n",
    "通过 find_element_by_css_selector( )函数，选择取百度输入框的 id 属性来定义\n",
    "也可以取 name 属性\n",
    "<a href=\"http://news.baidu.com\" name=\"tj_news\">新 闻</a>\n",
    "driver.find_element_by_css_selector(\"a[name=\\\"tj_news\\\"]\").click()\n",
    "\n",
    "\n",
    "可以取 title 属性\n",
    "<a onclick=\"queryTab(this);\" mon=\"col=502&pn=0\" title=\"web\"\n",
    "href=\"http://www.baidu.com/\">网页</a>\n",
    "\n",
    "driver.find_element_by_css_selector(\"a[title=\\\"web\\\"]\").click()\n",
    "\n",
    "\n",
    "也可以是取..:\n",
    "<a class=\"RecycleBin xz\" href=\"javascript:void(0);\">\n",
    "driver.find_element_by_css_selector(\"a.RecycleBin\").click()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# XPath 定位"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "xpath:attributer （属性）\n",
    "driver.find_element_by_xpath(\"//input[@id='kw']\").send_keys(\"selenium\")\n",
    "#input 标签下 id =kw 的元素\n",
    "\n",
    "xpath:idRelative （id 相关性）\n",
    "driver.find_element_by_xpath(\"//div[@id='fm']/form/span/input\").send_keys(\"s\n",
    "elenium\")\n",
    "#在/form/span/input 层级标签下有个 div 标签的 id=fm 的元素\n",
    "                                                                          \n",
    "driver.find_element_by_xpath(\"//tr[@id='check']/td[2]\").click()\n",
    "# id 为'check' 的 tr ，定位它里面的第2个 td\n",
    "                                                                          \n",
    "xpath:position （位置）\n",
    "driver.find_element_by_xpath(\"//input\").send_keys(\"selenium\")\n",
    "driver.find_element_by_xpath(\"//tr[7]/td[2]\").click()\n",
    "#第7个 tr 里面的第2个 td\n",
    "                                                                          \n",
    "xpath: href （水平参考）\n",
    "driver.find_element_by_xpath(\"//a[contains(text(),'网页')]\").click()\n",
    "#在 a 标签下有个文本（text）包含（contains）'网页' 的元素\n",
    "                                                                          \n",
    "xpath:link\n",
    "driver.find_element_by_xpath(\"//a[@href='http://www.baidu.com/']\").click()\n",
    "#有个叫 a 的标签，他有个链接 href='http://www.baidu.com/ 的元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 智能等待"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "\n",
    "selenium.webdriver.remote.webdriver.implicitly_wait(time_to_wait)\n",
    "\n",
    "隐式地等待一个无素被发现或一个命令完成；这个方法每次会话只需要调用一次time_to_wait: 等待时间\n",
    "    \n",
    "用法:\n",
    "browser.implicitly_wait(30)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 操作浏览器的前进、后退"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "#返回（后退）\n",
    "browser.back()\n",
    "\n",
    "#前进\n",
    "browser.forward()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 操作测试对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "webdriver 中比较常用的操作对象的方法有下面几个\n",
    "\n",
    " click 点击对象\n",
    "\n",
    " send_keys 在对象上模拟按键输入\n",
    "\n",
    " clear 清除对象的内容，如果可以的话\n",
    "\n",
    " submit 提交\n",
    "\n",
    " text 用于获取元素的文本信息\n",
    "\n",
    " get_attribute 获得属性值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 键盘按键用法"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.keys import Keys #需要引入 keys 包\n",
    "\n",
    "driver.maximize_window() # 浏览器全屏显示\n",
    "\n",
    "driver.find_element_by_id(\"user_name\").clear()\n",
    "\n",
    "driver.find_element_by_id(\"user_name\").send_keys(\"fnngj\")\n",
    "\n",
    "#tab 的定位相相于清除了密码框的默认提示信息，等同上面的 clear()\n",
    "driver.find_element_by_id(\"user_name\").send_keys(Keys.TAB)\n",
    "\n",
    "time.sleep(3)\n",
    "driver.find_element_by_id(\"user_pwd\").send_keys(\"123456\")\n",
    "#通过定位密码框，enter（回车）来代替登陆按钮\n",
    "driver.find_element_by_id(\"user_pwd\").send_keys(Keys.ENTER)\n",
    "\n",
    "'''\n",
    "#也可定位登陆按钮，通过 enter（回车）代替 click()\n",
    "driver.find_element_by_id(\"login\").send_keys(Keys.ENTER)\n",
    "'''\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 多层框架/窗口定位"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    " switch_to_frame()\n",
    " switch_to_window()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "#先找到到 ifrome1（id = f1）\n",
    "browser.switch_to_frame(\"f1\")\n",
    "#再找到其下面的 ifrome2(id =f2)\n",
    "browser.switch_to_frame(\"f2\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 一般对话框的处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "有些弹出对话框窗，我们可以通过判断是否为当前窗口的方式进行操作。\n",
    "\n",
    "#获得当前窗口\n",
    "nowhandle=driver.current_window_handle\n",
    "\n",
    "#打开弹窗\n",
    "driver.find_element_by_name(\"xxx\").click()\n",
    "#获得所有窗口\n",
    "allhandles=driver.window_handles\n",
    "\n",
    "for handle in allhandles:\n",
    "    if handle!=nowhandle: #比较当前窗口是不是原先的窗口\n",
    "    driver.switch_to_window(handle) #获得当前窗口的句柄\n",
    "    dirver.find_element_by_class_name(\"xxxx\").click() #在当前窗口操作"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "调用 js\n",
    "\n",
    " execute_script(script, *args)\n",
    "\n",
    "\n",
    "在当前窗口/框架 同步执行 javaScript\n",
    "    script：JavaScript 的执行。\n",
    "    args：适用任何 JavaScript 脚本。\n",
    "    \n",
    "使用：\n",
    "driver.execute_script（‘document.title’）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
